#include "AllocTest.h"
#include "Unit/MyAlloc.h"
#include <time.h>

AllocTest::AllocTest( void )
{

}

AllocTest::~AllocTest( void )
{

}
struct TestStruct
{
	int a1;
	int a2;
	int a3;
	TestStruct():a1(0),a2(0),a3(0){}
};
bool TestOccupyAlloc()
{
	printf("------------TestOccupyAlloc------------\n");
	int len = 1024000;
	std::vector<TestStruct*> pointSeq;
	pointSeq.resize(len);
	OccupyAlloc<TestStruct> occupAlloc;
	occupAlloc.Init(len);
	int loop = 10;
	std::vector<uint> allocUseTimeList;
	std::vector<uint> freeUseTimeList;
	allocUseTimeList.resize(loop);
	freeUseTimeList.resize(loop);
	uint uTime1,uTime2;
	for ( int i = 0; i < loop; i++)
	{
		uTime1 = GetTick();
		for ( int j = 0; j < len; j++ )
		{
			TestStruct* pStruct = occupAlloc.Alloc();
			if ( !pStruct ) {
				printf("occupAlloc.Alloc error i:%d\n", j);
				continue;
			}
			//printf("alloc i:%d\n", i);
			pointSeq[j] = pStruct;
		}
		uTime2 = GetTick();
		allocUseTimeList[i]=uTime2-uTime1;
		uTime1 = uTime2;

		len = pointSeq.size();
		for ( int j = 0; j < len; j++ )
		{
			TestStruct* pStruct = pointSeq[j];
			occupAlloc.Free(pStruct);
			//printf("free i:%d\n", i);
		}
		uTime2 = GetTick();
		freeUseTimeList[i]=uTime2-uTime1;
	}	
	for ( size_t i = 0; i < allocUseTimeList.size(); i++ )
	{
		printf("alloc %d use :%d free:%d\n", len, allocUseTimeList[i], freeUseTimeList[i]);
	}
	
	printf("------------TestOccupyAlloc------------\n\n");
	return true;
}
bool AllocTest::Test()
{
	printf("=================AllocTest=================\n\n");
	TestOccupyAlloc();
	printf("=================AllocTest=================\n\n");
	return true;
}
